package com.pay.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean;

/**
 * @Description: dubbo基础配置
 * @see: DubboConfig 此处填写需要参考的类
 * @version 2016年8月24日 下午6:21:05
 * @author jian.cao
 */
@Configuration
@PropertySource("classpath:/system.properties")
public class DubboConfig {

	@Value("${dubbo.appName}")
	private String appName;

	// pay_dubbo注册地址
	@Value("${dubbo.address}")
	private String resAddress;

	@Value("${dubbo.registryProtocol}")
	private String registryProtocol;

	@Value("${dubbo.protocolName}")
	private String protocolName;

	@Value("${dubbo.port}")
	private int port;

	@Value("${dubbo.connections}")
	private int connections;

	/**
     * 注入dubbo上下文
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        // 当前应用配置
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(this.appName);
        return applicationConfig;
    }
	
    /**
     * 注入dubbo注册中心配置,基于zookeeper
     * @return
     */
    @Bean(name = "pay") 
    @Primary
    public RegistryConfig payRegConfig() {
        // 连接注册中心配置
    	RegistryConfig registryConfig = new RegistryConfig(resAddress);
		registryConfig.setProtocol(registryProtocol);
		registryConfig.setPort(port);
		registryConfig.setDefault(true);
		registryConfig.setId("pay");
        return registryConfig;
    }

	@Bean
	public ProtocolConfig protocol() {
		ProtocolConfig protocolConfig = new ProtocolConfig(protocolName, port);
		return protocolConfig;
	}

	@Bean
	public MonitorConfig monitor() {
		MonitorConfig monitorConfig = new MonitorConfig();
		monitorConfig.setProtocol("registry");
		return monitorConfig;
	}

	@Bean
	public ProviderConfig provider() {
		ProviderConfig config = new ProviderConfig();
		config.setConnections(connections);
		return config;
	}
	
//    @Bean
//    public ServiceBean<WxCustomerServiceFacade> wxCustomerServiceFacadeDubbo(WxCustomerServiceFacade wxCustomerServiceFacade) {
//        ServiceBean<WxCustomerServiceFacade> serviceBean = new ServiceBean<WxCustomerServiceFacade>();
//        serviceBean.setInterface(WxCustomerServiceFacade.class.getName());
//        serviceBean.setRef(wxCustomerServiceFacade);
//        serviceBean.setTimeout(1200000);
//        return serviceBean;
//    }


	
	/**
	 * 结算信息
	 * @return
	 */
//    @Bean(name="posMisQueryFacade")
//    public ReferenceBean<PosMisQueryFacade> posMisQueryFacade() {
//        ReferenceBean<PosMisQueryFacade> ref = new ReferenceBean<PosMisQueryFacade>();
//        ref.setInterface(PosMisQueryFacade.class);
//        ref.setTimeout(5000);
//        ref.setRegistry(payRegConfig());
//        ref.setCheck(false);
//        return ref;
//    }

    
}
